package com.sdk.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TouchCircleView extends View {
	private static final int[] CIRCLE_COLORS = new int[] { 0xFFFF0000,
			0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00,
			0xFFFF0000 };
	private static final int DEFAULT_CENTER_COLOR = Color.GREEN;
	private static final int DEFAULT_BACKGROUND_STROKE_WIDTH = 4;
	private static final int DEFAULT_CENTER_STROKE_WIDTH = 4;
	private static final int CENTER_RADIUS = 50;
	private Paint mBackgroundPaint;
	private Paint mCenterPaint;
	private Paint mToucheViewPaint;
	private int mWidth = 0;
	private int mHeight = 0;
	private int mCenterColor = DEFAULT_CENTER_COLOR;
	private Shader mShader;
	// 滑动圆的半径
	private int mTouchViewRadius = 0;
	private static final int TOUCH_VIEW_OFFSET = 15;
	private int mBackgroundCircleRadius = 0;
	private int mTouchCircleX = 0;
	private int mTouchCircleY = 0;

	public TouchCircleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initResource();
	}

	public TouchCircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initResource();
	}

	public TouchCircleView(Context context) {
		super(context);
		initResource();
	}

	private void initResource() {
		initBackgroundPaint();
		initCenterPaint();
		initTouchViewPaint();
		setFocusable(true);
		setFocusableInTouchMode(true);
	}

	/**
	 * 初始化背景的Paint属性
	 */
	private void initBackgroundPaint() {
		mShader = new SweepGradient(0, 0, CIRCLE_COLORS, null);
		// 抗锯齿
		mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mBackgroundPaint.setStyle(Paint.Style.FILL);
		mBackgroundPaint.setStrokeWidth(DEFAULT_BACKGROUND_STROKE_WIDTH);
	}

	/**
	 * 初始Touch圆的Paint属性
	 */
	private void initTouchViewPaint() {
		mToucheViewPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mToucheViewPaint.setStyle(Paint.Style.FILL);
		mToucheViewPaint.setColor(Color.BLACK);
	}

	/**
	 * 初始化背景的Paint属性
	 */
	private void initCenterPaint() {
		mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mCenterPaint.setColor(mCenterColor);
		mCenterPaint.setStrokeWidth(DEFAULT_CENTER_STROKE_WIDTH);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		canvas.drawCircle(mWidth / 2, mHeight / 2, mBackgroundCircleRadius,
				mBackgroundPaint);
		canvas.drawCircle(mWidth / 2, mHeight / 2, CENTER_RADIUS, mCenterPaint);
		canvas.drawCircle(mTouchCircleX, mTouchCircleY, mTouchViewRadius,
				mToucheViewPaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		mWidth = getWidth();
		mHeight = getHeight();
		mShader = new SweepGradient(mWidth / 2, mHeight / 2, CIRCLE_COLORS,
				null);
		mBackgroundCircleRadius = mWidth > mHeight ? mHeight / 2 : mWidth / 2;
		mBackgroundPaint.setShader(mShader);
		mTouchViewRadius = (mBackgroundCircleRadius - CENTER_RADIUS) / 2
				- TOUCH_VIEW_OFFSET;
		mTouchCircleX = mWidth
				/ 2
				- (int) ((mBackgroundCircleRadius - TOUCH_VIEW_OFFSET - mTouchViewRadius) * Math
						.sin(mDegree));
		mTouchCircleY = mHeight
				/ 2
				- (int) ((mBackgroundCircleRadius - TOUCH_VIEW_OFFSET - mTouchViewRadius) * Math
						.cos(mDegree));
	}

	/**
	 * 设置中间圆球的颜色
	 * 
	 * @param centerColor
	 */
	public void setCenterColor(int centerColor) {
		this.mCenterColor = centerColor;
		postInvalidate();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			if (checkPoint(event)) {
				float pointX = event.getX();
				float pointY = event.getY();
				mDegree = Math.atan2(pointY - mHeight / 2, pointX - mWidth / 2);
				mTouchCircleX = mWidth
						/ 2
						- (int) ((mBackgroundCircleRadius + CENTER_RADIUS) / 2 * Math
								.cos(mDegree + Math.PI));
				mTouchCircleY = mHeight
						/ 2
						- (int) ((mBackgroundCircleRadius + CENTER_RADIUS) / 2 * Math
								.sin(mDegree + Math.PI));
				int percent = (int) Math.abs((mDegree) / Math.PI * 100);
				if (null != mTrackBallMoveListener) {
					mTrackBallMoveListener.onTrackBallMove(percent);
				}
				invalidate();
			}
			break;

		default:
			break;
		}
		return true;
	}

	private boolean checkPoint(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		double distanceToCenter = Math.sqrt(Math.pow(Math.abs(mHeight / 2 - y),
				2) + Math.pow(Math.abs(mWidth / 2 - x), 2));
		if (CENTER_RADIUS < distanceToCenter
				&& distanceToCenter < mBackgroundCircleRadius) {
			return true;
		}
		return false;
	}

	private double mDegree = 0;

	public void setDegree(float degree) {
		this.mDegree = degree;
		invalidate();
	}

	public interface OnTrackBallMoveListener {
		public void onTrackBallMove(int percent);
	}

	private OnTrackBallMoveListener mTrackBallMoveListener;

	public void setOnTrackBallMoveListener(
			OnTrackBallMoveListener onTrackBallMoveListener) {
		this.mTrackBallMoveListener = onTrackBallMoveListener;
	}
}
